//////////////////////////////////////////////////////////////////////////////////
//	Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#include <wx/wx.h>
#include "notifyoptions.h"

enum cComboID
{
	cComboID=12001,
	cTextSMTPSrvID,
	cTextSMTPSrvPortID,
	cTextSMTPUserID,
	cTextSMTPPasswID,
	cTextSMTPFromID,
	cTextSMTPToID
};


BEGIN_EVENT_TABLE(CNotifyOptions, wxPanel)
	EVT_TEXT(cComboID, 	CNotifyOptions::OnTimeIntChanged)
	EVT_COMMAND_RANGE(cTextSMTPSrvID, cTextSMTPToID, wxEVT_COMMAND_TEXT_UPDATED, CNotifyOptions::OnTextFieldsUpdated)
END_EVENT_TABLE()


CNotifyOptions::CNotifyOptions(wxWindow* pParent):wxPanel( pParent, wxID_ANY ), m_tInt(0)
{
	int nFlags      = wxLEFT|wxUP|wxRIGHT|wxALIGN_CENTRE_VERTICAL;
	int nBorder		= 5;

	wxFlexGridSizer* pSizer = new wxFlexGridSizer(1, 2, 5, 5);

	//E-mail
	wxStaticBoxSizer* pMailBoxSizer = new wxStaticBoxSizer( new
		wxStaticBox(this, wxID_ANY,  _("E-mail")), wxVERTICAL);

	wxFlexGridSizer* pMailSizer = new wxFlexGridSizer(6, 2, 5, 5);

	wxStaticText *ptlServer = new wxStaticText(this, -1, _("SMTP Server:"));
	wxStaticText *ptlMPort  = new wxStaticText(this, -1, _("SMTP Port:"));
	wxStaticText *ptlUsr    = new wxStaticText(this, -1, _("User:"));
	wxStaticText *ptlPassw  = new wxStaticText(this, -1, _("Password:"));
	wxStaticText *ptlFrom   = new wxStaticText(this, -1, _("From:"));
	wxStaticText *ptlTo		= new wxStaticText(this, -1, _("To:"));

	m_strSMTPPort = wxT("25");

	m_ptcSMTPSrv 	= NULL;
	m_ptcSMTPSrvPort= NULL;
	m_ptcSMTPUser 	= NULL;
	m_ptcSMTPPassw 	= NULL;
	m_ptcSMTPFrom 	= NULL;
	m_ptcSMTPTo 	= NULL;
	
	m_ptcSMTPSrv 	= new wxTextCtrl(this, cTextSMTPSrvID, wxT("") );
	m_ptcSMTPSrvPort= new wxTextCtrl(this, cTextSMTPSrvPortID, wxT(""),  wxDefaultPosition, wxDefaultSize, 0, wxTextValidator(wxFILTER_NUMERIC, &m_strSMTPPort));
	m_ptcSMTPUser 	= new wxTextCtrl(this, cTextSMTPUserID, wxT("") );
	m_ptcSMTPPassw 	= new wxTextCtrl(this, cTextSMTPPasswID, wxT(""), wxDefaultPosition, wxDefaultSize, wxTE_PASSWORD);
	m_ptcSMTPFrom 	= new wxTextCtrl(this, cTextSMTPFromID, wxT("") );
	m_ptcSMTPTo 	= new wxTextCtrl(this, cTextSMTPToID, wxT("") );

	pMailSizer->Add(ptlServer, 0,wxALIGN_CENTRE_VERTICAL);
	pMailSizer->Add(m_ptcSMTPSrv, 1, wxEXPAND);

	pMailSizer->Add(ptlMPort, 0,wxALIGN_CENTRE_VERTICAL);
	pMailSizer->Add(m_ptcSMTPSrvPort, 1, wxEXPAND);

	pMailSizer->Add(ptlUsr, 0, wxALIGN_CENTRE_VERTICAL);
	pMailSizer->Add(m_ptcSMTPUser, 1, wxEXPAND);
		
	pMailSizer->Add(ptlPassw, 0, wxALIGN_CENTRE_VERTICAL);
	pMailSizer->Add(m_ptcSMTPPassw, 1, wxEXPAND);

	pMailSizer->Add(ptlFrom, 0, wxALIGN_CENTRE_VERTICAL);
	pMailSizer->Add(m_ptcSMTPFrom, 1, wxEXPAND);

	pMailSizer->Add(ptlTo, 0, wxALIGN_CENTRE_VERTICAL);
	pMailSizer->Add(m_ptcSMTPTo, 1, wxEXPAND);
	
	pMailSizer->AddGrowableCol(1, 1);
	pMailBoxSizer->Add(pMailSizer, 1, wxALIGN_CENTER|wxALL, 5);
	pSizer->Add( pMailBoxSizer, 1, wxALL|wxEXPAND, 5 );

	//Notify options
	wxStaticBoxSizer* pNotifyBoxSizer = new wxStaticBoxSizer( new
		wxStaticBox(this, wxID_ANY,  _("Send Options")), wxVERTICAL);

	m_pSendAppLog = new wxCheckBox(this, wxID_ANY, _("Send application Log") );
	pNotifyBoxSizer->Add( m_pSendAppLog, 0, nFlags, nBorder );
	pNotifyBoxSizer->AddSpacer(10);

	////////////////////////////////////////////////////////////////////////////////
	//Syslog notify
	wxBoxSizer* pSysLogSizer = new wxBoxSizer(wxHORIZONTAL);
	m_pSendSysLog = new wxCheckBox(this, wxID_ANY, _("Send Syslog, priority:") );
	pSysLogSizer->Add(m_pSendSysLog, 0, nFlags, nBorder );

	m_pSyslogLevel = new wxComboBox(this, 	wxID_ANY, _("All"), wxDefaultPosition, wxDefaultSize, 0, NULL, wxCB_DROPDOWN|wxCB_READONLY );
	m_pSyslogLevel->Append ( _("Emergency"),	(void*)0);
	m_pSyslogLevel->Append ( _("Alert"),		(void*)1);
	m_pSyslogLevel->Append ( _("Critical"),		(void*)2);
	m_pSyslogLevel->Append ( _("Error"),		(void*)3);
	m_pSyslogLevel->Append ( _("Warning"),		(void*)4);
	m_pSyslogLevel->Append ( _("Notice"),		(void*)5);
	m_pSyslogLevel->Append ( _("Informational"),(void*)6);
	m_pSyslogLevel->Append ( _("Debug"),		(void*)7);
	m_pSyslogLevel->Select(0);
	pSysLogSizer->Add(m_pSyslogLevel, 0, nFlags|wxALIGN_RIGHT, nBorder );

	pNotifyBoxSizer->Add( pSysLogSizer, 0, wxALL|wxEXPAND|wxALIGN_CENTER_VERTICAL, 0 );
	pNotifyBoxSizer->AddSpacer(10);
	////////////////////////////////////////////////////////////////////////////////

	wxStaticText* pTxt = new wxStaticText(this, wxID_ANY, _("Send interval:"));
	m_pcSendTime  = new wxComboBox(this, cComboID, wxT(""), wxDefaultPosition, wxSize(150, wxDefaultSize.GetY()), 0, NULL, wxCB_DROPDOWN|wxCB_READONLY  );

	m_pcSendTime->Append( _("Immediate"),			(void*)0);
	m_pcSendTime->Append( _("Once per minute"),		(void*)60);
	m_pcSendTime->Append( _("Once per hour"),		(void*)3600);

	m_pcSendTime->SetSelection(0);

	pNotifyBoxSizer->AddSpacer(10);

	wxBoxSizer* psCombo = new wxBoxSizer(wxHORIZONTAL);

	psCombo->Add( pTxt, 0, nFlags, nBorder );
	psCombo->Add( m_pcSendTime, nFlags|wxALIGN_RIGHT, nBorder );

	pNotifyBoxSizer->Add(psCombo, 0, wxALIGN_CENTER, 5);
	pSizer->Add( pNotifyBoxSizer, 1, wxALL|wxEXPAND, 5 );

	SetSizer( pSizer );
}

CNotifyOptions::~CNotifyOptions(void)
{
}

void CNotifyOptions::OnTimeIntChanged(wxCommandEvent&)
{
	void* pData = m_pcSendTime->GetClientData( m_pcSendTime->GetCurrentSelection());
	m_tInt = (long)pData;
}

void CNotifyOptions::OnTextFieldsUpdated(wxCommandEvent& ev)
{
	/*
	if (!m_strSMTPSrv ||
		!m_strSMTPPort||
		!m_strSMTPUser||
		!m_strSMTPPassw||
		!m_strSMTPFrom||
		!m_strSMTPTo)
	{
		return;
	}*/

	/*
	m_strSMTPSrv = m_ptcSMTPSrv->GetValue();
	m_strSMTPPort = m_ptcSMTPSrvPort->GetValue();
	m_strSMTPUser = m_ptcSMTPUser->GetValue();
	m_strSMTPPassw = m_ptcSMTPPassw->GetValue(); 
	m_strSMTPFrom = m_ptcSMTPFrom->GetValue(); 
	m_strSMTPTo = m_ptcSMTPTo->GetValue();
	*/
}

//SMTP Server
wxString CNotifyOptions::GetSMTPServer()
{
	return m_ptcSMTPSrv->GetValue();
}

void CNotifyOptions::SetSMTPServer(const wxString& str)
{
	m_ptcSMTPSrv->SetValue(str);
}

//SMTP Server Port
unsigned short CNotifyOptions::GetSMTPServerPort()
{
	unsigned short wPort = (unsigned short)atol( m_ptcSMTPSrvPort->GetValue() );

	return wPort?wPort:25;
}

void CNotifyOptions::SetSMTPServerPort(unsigned short wPort)
{
	m_strSMTPPort = wxString::Format(wxT("%u"), (unsigned int)wPort);
	m_ptcSMTPSrvPort->SetValue(m_strSMTPPort);
}


//SMTP User
wxString CNotifyOptions::GetSMTPUser()
{
	return m_ptcSMTPUser->GetValue();
}

void CNotifyOptions::SetSMTPUser(const wxString& str)
{
	m_ptcSMTPUser->SetValue(str);
}

//SMTP Passw
wxString CNotifyOptions::GetSMTPPassw()
{
	return m_ptcSMTPPassw->GetValue();
}

void CNotifyOptions::SetSMTPPassw(const wxString& str)
{
	m_ptcSMTPPassw->SetValue(str);
}

//SMTP From
wxString CNotifyOptions::GetSMTPFrom()
{
	return m_ptcSMTPFrom->GetValue();
}

void  CNotifyOptions::SetSMTPFrom(const wxString& str)
{
	m_ptcSMTPFrom->SetValue(str);
}

//SMTP To
wxString CNotifyOptions::GetSMTPTo()
{
	return m_ptcSMTPTo->GetValue();
}

void CNotifyOptions::SetSMTPTo(const wxString& str)
{
	m_ptcSMTPTo->SetValue(str);
}

//Send application Log
bool CNotifyOptions::GetSendAppLog()
{
	return m_pSendAppLog->GetValue();
}

void CNotifyOptions::SetSendAppLog(bool bVal)
{
	m_pSendAppLog->SetValue( bVal );
}

//Send syslog Log
bool CNotifyOptions::GetSendSysLog()
{
	return m_pSendSysLog->GetValue();
}

void CNotifyOptions::SetSendSysLog(bool bVal)
{
	m_pSendSysLog->SetValue( bVal );
}

//Send interval
time_t CNotifyOptions::GetSendPeriod()
{
	return m_tInt;
}

void CNotifyOptions::SetSendPeriod(time_t t)
{
	m_tInt = t;
	
	void* pData;
	
	for ( size_t i=0;i<m_pcSendTime->GetCount();i++ )
	{
		pData = m_pcSendTime->GetClientData( i );
		
		if ( m_tInt == (long)pData )
		{
			m_pcSendTime->SetSelection( i );
			break;
		}
	}
}

//Syslog level
long CNotifyOptions::GetSyslogSendLevel()
{
	long lRet(0);

	void* pData = m_pSyslogLevel->GetClientData( m_pSyslogLevel->GetCurrentSelection());
	lRet = (long)pData;

	return lRet;
}

void CNotifyOptions::SetSyslogSendLevel(long lLevel)
{
	void* pData;
	
	for ( size_t i=0;i<m_pSyslogLevel->GetCount();i++ )
	{
		pData = m_pSyslogLevel->GetClientData( i );
		
		if ( lLevel == (long)pData )
		{
			m_pSyslogLevel->SetSelection( i );
			break;
		}
	}
}

